<?php

namespace App\Console\Commands;

use App\Models\fund\Rebate;
use App\Models\User;
use App\Models\user\UserSuperior;
use Illuminate\Console\Command;

class SendReward extends Command
{
    /**
     *
     * 非树洞代码 暂时不使用
     *
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'SendReward';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'SendReward maintain command ';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     *
     * 结算奖励  每月25号执行
     *
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        /* 永不超时 */
        ini_set('max_execution_time', 0);


        $map['status'] = 0;

        Rebate::where($map)
            ->whereYear('created_at',date('Y'))
            ->whereMonth('created_at',date('m')-1)
            ->get()->each(function (Rebate $rebate){
            $rebate->status = 1;
            $rebate->save();
            User::whereId($rebate->relation_user_id)->increment('money',$rebate->amount);
            $map['user_id'] = $rebate->user_id;
            $map['superior_id'] = $rebate->relation_user_id;
            UserSuperior::where($map)->increment('rebate',$rebate->amount);
        });

    }



}
